/*
 * @Author: Marte
 * @Date:   2016-10-25 18:06:51
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-02 17:09:27
 */

'use strict';

function getParamByName(name) {
    var url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

//重设模态框样式
function setModalCss() {
    //按钮底边距为(图片盒高度-按钮高度)/2
    var imgBox = $('.imgbox'),
        btnPrev = $('.btn-prev'),
        btnNext = $('.btn-next');

    var marginHeight = (imgBox.innerHeight() - btnPrev.outerHeight()) / 2;
    btnPrev.css('margin-bottom', marginHeight + 'px');
    btnNext.css('margin-bottom', marginHeight + 'px');
    /*当图片盒宽度+按钮宽度>模态框体宽度时，模态框体宽度=图片盒宽度+按钮宽度+84*/
    if (btnPrev.outerWidth(true) * 2 + imgBox.outerWidth(true) + 104 > 900) {
        var modalWidth = btnPrev.outerWidth(true) * 2 + imgBox.outerWidth(true) + 104;
        $('.modal-dialog').css('width', modalWidth + 'px');
    } else {
        $('.modal-dialog').css('width', '900px');
    }
}

var currApp = angular.module('piclist.app', ['ui.bootstrap']);

currApp.controller('pagectrl', ['$scope', '$http', '$window', '$modal',
    function($scope, $http, $window, $modal) {
        var vm = this;
        var typeId = getParamByName('typeid');
        var remarkId = getParamByName('remarkid');

        vm.type = getParamByName('type');
        vm.remark = getParamByName('remark');

        $http.get('/api/picTypeRemarkGroups/groupbygroupname?typeid=' + typeId + '&remarkid=' + remarkId)
            .success(function(data, status, headers, config) {
                vm.picArr = data;
            })
            .error(function(data, status, headers, config) {
                vm.picArr = {};
            });

        $scope.showImage = function(groupName, index) { //点击图片时，打开模态框
            var modalInstance = $modal.open({
                templateUrl: 'imgbox.html', //指向上面创建的视图
                controller: 'ModalInstanceCtrl', // 初始化模态范围
                size: 'lg', //大小配置
                resolve: {
                    items: function() {
                        return { currGroup: vm.picArr[groupName], currIndex: index };
                    }
                }
            });
            modalInstance.result.then(function(params) {
                //关闭时处理
            }, function() {
                //dismiss时处理
            });
        };
    }
]);

currApp.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', 'items', '$timeout', function($scope, $modalInstance, items, $timeout) {
    //打开模态框，显示当前选中照片

    $scope.currIndex = items.currIndex;
    $scope.getCurrPic = function(index) {
        return items.currGroup[index];
    };

    $scope.prevImg = function() {
        $scope.currIndex = ($scope.currIndex === 0) ? 0 : ($scope.currIndex - 1);
    };

    $scope.nextImg = function() {
        $scope.currIndex = ($scope.currIndex === items.currGroup.length - 1) ? items.currGroup.length - 1 : ($scope.currIndex + 1);
    };

    $scope.getCurrPic = function(index) {
        return items.currGroup[index];
    };

    $scope.close = function() {
        $modalInstance.dismiss('cancel');
    };

    $scope.$watch('currIndex', function() {
        //图像变化时,重算图片按钮居中
        $timeout(function() { setModalCss(); }, 200);
    });
}]);
